package com.diancan;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import com.Utils.CustomViewBinder;
import com.Utils.MenuUtils;
import com.custom.ClassListViewWidget;
import com.custom.GrapeGridview;
import com.custom.MySimpleAdapter;
import com.declare.Declare;
import com.download.HttpDownloader;
import com.mode.CategoryObj;
import com.mode.DeskObj;
import com.mode.MenuObj;
import com.mode.SelectedMenuObj;
import com.mode.SelectedProduct;
import com.model.Category;

import android.R.integer;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Display;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MyTable extends Activity {
	LinearLayout rootLayout;
	LinearLayout rootLyt;
	ListView selectListView;
	SelectedProduct selectedProduct;
	public Hashtable<String, ClassListViewWidget> dicWidgets;
	Button selTableButton;
	Button commitButton;
	Button overButton;
	TextView sumTextView;
	BroadcastReceiver receiver;
	boolean isSelf=false;
	TextView txtTableName;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.mytable);
	txtTableName=(TextView)findViewById(R.id.table_name_txt);
	rootLayout=(LinearLayout)findViewById(R.id.rootLayout);
	rootLyt=(LinearLayout)findViewById(R.id.rootLyt);
	sumTextView=(TextView)findViewById(R.id.sumText);
	sumTextView.setTextColor(Color.DKGRAY);
	sumTextView.setTextSize(30);
	commitButton=(Button)findViewById(R.id.commitbtn);
	commitButton.setText(getResources().getString(R.string.btnstr_commit));
	commitButton.setOnClickListener(new ButtonOnclick());
	
	selTableButton=(Button)findViewById(R.id.btn_select_table);
	selTableButton.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			intent.setClass(MyTable.this,TableListPage.class);
			MyTable.this.startActivity(intent);
		}
	});
	
	overButton=(Button)findViewById(R.id.overbtn);
	overButton.setText(getResources().getString(R.string.btnstr_over));
	overButton.setOnClickListener(new OverBtnOnclick());
	overButton.setVisibility(View.GONE);
	
	dicWidgets=new Hashtable<String, ClassListViewWidget>();
	Declare declare=(Declare)getApplicationContext();
	if(declare.curDeskObj!=null)
	{
		txtTableName.setText(declare.curDeskObj.getName());
		selTableButton.setVisibility(View.GONE);
		commitButton.setVisibility(View.VISIBLE);
		CreateElements();
	}
	else {
		selTableButton.setVisibility(View.VISIBLE);
		commitButton.setVisibility(View.GONE);
	}	
	
//	//广播接收器
//    receiver = new BroadcastReceiver() {
//	  	@Override
//	      public void onReceive(Context ctx, Intent intent) {
//	  		if (intent.getAction().equals("menuedit")) {
//	  			SelectedMenuObj sMenuObj=(SelectedMenuObj) intent.getSerializableExtra("menu");
//	  			if(!isSelf)
//	  			{
//	  				String strKey;
//	  		    	Enumeration<String> meenum = dicWidgets.keys();
//	  		    	while (meenum.hasMoreElements()) {
//	  		    		strKey=meenum.nextElement();
//	  					ClassListViewWidget clvw=dicWidgets.get(strKey);
//	  					if(clvw.isEmpty())
//	  					{
//	  						dicWidgets.remove(strKey);
//	  						rootLayout.removeView(clvw);
//	  					}
//	  					else {
//							ListView listView=clvw.getListView();
//							clvw.setListViewHeight(listView);
//						}
//	  				}
//	  				sumTextView.setText(getResources().getString(R.string.infostr_sum)+selectedProduct.getSum());
//	  			}
//	  		}
//	  	}
//	  };
//	  IntentFilter filter = new IntentFilter();
//	  filter.addAction("menuedit");
//	  filter.addCategory(Intent.CATEGORY_DEFAULT);
//	  registerReceiver(receiver, filter);
    }
    @Override
	protected void onResume() {
		// TODO Auto-generated method stub
    	Declare declare=(Declare)getApplicationContext();
    	if(declare.curDeskObj!=null)
    	{
    		selTableButton.setVisibility(View.GONE);
    		commitButton.setVisibility(View.VISIBLE);
    		overButton.setVisibility(View.GONE);
    		txtTableName.setText(declare.curDeskObj.getName());		
    		if(dicWidgets.size()<=0)
    		{
    			CreateElements();
    		}
    		else {
    			UpdateElement();
			} 		
    	}
    	else {
    		selTableButton.setVisibility(View.VISIBLE);
    		commitButton.setVisibility(View.GONE);
    		overButton.setVisibility(View.GONE);
		}
    	
		super.onResume();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	private void UpdateHashList(List<SelectedMenuObj> menuInfos,ArrayList<HashMap<String, Object>> hashList)
	{
		hashList.clear();
		for (Iterator iterator = menuInfos.iterator(); iterator.hasNext();) {
			SelectedMenuObj menuInfo = (SelectedMenuObj) iterator.next();
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("title", menuInfo.getName());
			map.put("price", menuInfo.getPrice()+"");
			String strCount=menuInfo.getCount()+"";
			map.put("count", strCount);
			//����ͼƬ
			Bitmap addbmp=BitmapFactory.decodeResource(
					this.getResources(), R.drawable.addimg);
			Bitmap deletebmp=BitmapFactory.decodeResource(
					this.getResources(), R.drawable.deleteimg);
			map.put("imgdelete", deletebmp);
			map.put("imgadd", addbmp);
			hashList.add(map);
		}
	}
    private void CreateElements()
    {
    	Declare declare=(Declare)getApplicationContext();
    	selectedProduct=declare.curDeskObj.getSelectedProduct();
    	String strKey;
    	Set<String> meenum = selectedProduct.getDicMenusHashtable().keySet();
    	
    	Iterator iterator;
		for (iterator = meenum.iterator(); iterator.hasNext();) {
			strKey = (String) iterator.next();
			List<SelectedMenuObj> selectedList=selectedProduct.getDicMenusHashtable().get(strKey);
			ClassListViewWidget clvw=CreateListWidget(selectedList, strKey);
			clvw.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
			clvw.setScrollContainer(true);
			rootLayout.addView(clvw);
			dicWidgets.put(strKey, clvw);
			
			registerForContextMenu(clvw.getListView());
		}
    	sumTextView.setText(getResources().getString(R.string.infostr_sum)+selectedProduct.getSum());
    	
    	if(!selectedProduct.isbState())
    	{
    		commitButton.setVisibility(View.VISIBLE);
    		overButton.setVisibility(View.GONE);
    	}
    	else {
			commitButton.setVisibility(View.GONE);
			overButton.setVisibility(View.VISIBLE);
		}
    	if(meenum.size()<=0)
    	{
    		commitButton.setVisibility(View.INVISIBLE);
    		overButton.setVisibility(View.GONE);
    	}
    	
    }
    private void  UpdateElement() {
    	Declare declare=(Declare)getApplicationContext();
    	if(declare.curDeskObj==null)
    	{
			dicWidgets.clear();
			rootLayout.removeAllViews();
			selTableButton.setVisibility(View.VISIBLE);
			commitButton.setVisibility(View.GONE);
			sumTextView.setText("");
			txtTableName.setText("");
    		return;
    	}
    	selectedProduct=declare.curDeskObj.getSelectedProduct();
    	
    	String strKey;
    	Set<String> meenum;
    	if(selectedProduct.getDicMenusHashtable().size()>=dicWidgets.size())
    	{
    		meenum= selectedProduct.getDicMenusHashtable().keySet();
    		Iterator iterator;
    		for (iterator = meenum.iterator(); iterator.hasNext();) {
    			strKey = (String) iterator.next();
    			if(dicWidgets.containsKey(strKey))
        		{
        			ClassListViewWidget classListViewWidget=dicWidgets.get(strKey);
        			TableListAdapter sListAdapter=(TableListAdapter)classListViewWidget.listView.getAdapter();
        			ArrayList<HashMap<String, Object>> hashList=classListViewWidget.getHashList();
        			List<SelectedMenuObj> selectedList=selectedProduct.getDicMenusHashtable().get(strKey);
        			UpdateHashList(selectedList, hashList);
        			sListAdapter.setmItemList(hashList);
        			sListAdapter.notifyDataSetChanged();
        			sListAdapter.setMenuList(selectedList);
        			classListViewWidget.setListViewHeight(classListViewWidget.getListView());
        		}
        		else {
        			List<SelectedMenuObj> selectedList=selectedProduct.getDicMenusHashtable().get(strKey);
        			ClassListViewWidget clvw=CreateListWidget(selectedList, strKey);
        			clvw.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        			clvw.setScrollContainer(true);
        			rootLayout.addView(clvw);
        			dicWidgets.put(strKey, clvw);
        			registerForContextMenu(clvw.getListView());
				}
    		}
        	
    	}
    	else {
    		meenum= dicWidgets.keySet();
    		Iterator iterator;
    		for (iterator = meenum.iterator(); iterator.hasNext();) {
    			strKey = (String) iterator.next();
    			if(selectedProduct.getDicMenusHashtable().containsKey(strKey))
        		{
        			ClassListViewWidget classListViewWidget=dicWidgets.get(strKey);
        			TableListAdapter sListAdapter=(TableListAdapter)classListViewWidget.listView.getAdapter();
        			ArrayList<HashMap<String, Object>> hashList=classListViewWidget.getHashList();
        			List<SelectedMenuObj> selectedList=selectedProduct.getDicMenusHashtable().get(strKey);
        			UpdateHashList(selectedList, hashList);
        			sListAdapter.setmItemList(hashList);
        			sListAdapter.notifyDataSetChanged();
        			sListAdapter.setMenuList(selectedList);
        			classListViewWidget.setListViewHeight(classListViewWidget.getListView());
        		}
        		else {
        			ClassListViewWidget classListViewWidget1=dicWidgets.get(strKey);
        			rootLayout.removeView(classListViewWidget1);
        			dicWidgets.remove(strKey);
				}
    		}
		}
    	
    	sumTextView.setText(getResources().getString(R.string.infostr_sum)+selectedProduct.getSum());
    	if(!selectedProduct.isbState())
    	{
    		commitButton.setVisibility(View.VISIBLE);
    		overButton.setVisibility(View.GONE);
    	}
    	else {
			commitButton.setVisibility(View.GONE);
			overButton.setVisibility(View.VISIBLE);
		}
    	if(selectedProduct.getDicMenusHashtable().size()<=0)
    	{
    		commitButton.setVisibility(View.INVISIBLE);
    		overButton.setVisibility(View.GONE);
    	}
	}
    private ClassListViewWidget CreateListWidget(List<SelectedMenuObj> menulist,String strtitle)
    {
    	ArrayList<HashMap<String, Object>> hashList=new ArrayList<HashMap<String, Object>>();
    	UpdateHashList(menulist, hashList);
    	TableListAdapter simpleAdapter1=new TableListAdapter(this, hashList,
				R.layout.select_list_item, new String[] { "title", "price","count","imgdelete","imgadd" },
				new int[] { R.id.mtitle, R.id.mprice,R.id.mcount,R.id.imgdelete,R.id.imgadd});
    	simpleAdapter1.setMenuList(menulist);
    	Declare d=(Declare)getApplicationContext();
    	String sCategoryString="";
    	List<CategoryObj> cList=d.getMenuListDataObj().getCategoryObjs();
    	Iterator<CategoryObj> iterator;
    	for(iterator=cList.iterator();iterator.hasNext();)
    	{
    		CategoryObj category=iterator.next();
    		if((category.getId()+"").equals(strtitle))
    		{
    			sCategoryString=category.getName();
    			break;
    		}
    	}
    	ClassListViewWidget cLvWidget=new ClassListViewWidget(this, menulist, hashList, sCategoryString);
    	simpleAdapter1.setViewBinder(new CustomViewBinder());
    	cLvWidget.listView.setAdapter(simpleAdapter1);
    	setListViewHeight(cLvWidget.listView);
    	return cLvWidget;
    }

//    //�����˵�
//    @Override 
//    public void onCreateContextMenu(ContextMenu menu, View v, 
//            ContextMenuInfo menuInfo) { 
//        super.onCreateContextMenu(menu, v, menuInfo); 
////        selectGrapeGridview=(GrapeGridview)v;
//        selectListView=(ListView)v;
//        if(!selectedProduct.isbState())
//        {
//        	menu.setHeaderTitle(getResources().getString(R.string.title_contextmenu));
//            menu.add(0, 1, 0, getResources().getString(R.string.menustr_delete)); 
//        }
//    }
    
//    //ѡ�в˵���
//    @Override 
//    public boolean onContextItemSelected(MenuItem item) { 
//    	AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo(); 
//    	if(item.getItemId()==1)
//    	{
//    		int pos = (int) selectListView.getAdapter().getItemId(menuInfo.position);
//    		SelectedMenuObj menu;
//    		TableListAdapter tableListAdapter=(TableListAdapter)selectListView.getAdapter();
//    		tableListAdapter.getmItemList().remove(pos);
//    		menu=tableListAdapter.getMenuList().remove(pos);
//    		tableListAdapter.notifyDataSetChanged();
//    		setListViewHeight(selectListView);
//    		selectedProduct.DeleteMenu(menu);
//			menu.setCount(0);
//			menu.setTotalPrice(0);
//			Declare declare=(Declare)getApplicationContext();
//			declare.getMenuListDataObj().ChangeMenuListByObj(menu);
//			declare.getMenuListDataObj().ChangeHashList(menu);
//			sumTextView.setText(getResources().getString(R.string.infostr_sum)+selectedProduct.getSum());
//			MenuUtils.bUpdating=true;
//			isSelf=true;
//			SendMessage(menu);
//    	}
//        return super.onContextItemSelected(item); 
//    }
    public void setListViewHeight(ListView lv) {
        ListAdapter la = lv.getAdapter();
        if(null == la) {
            return;
        }
        // calculate height of all items.
        int h = 0;
        final int cnt = la.getCount();
        for(int i=0; i<cnt; i++) {
            View item = la.getView(i, null, lv);
            item.measure(0, 0);
            h += item.getMeasuredHeight();
        }
        // reset ListView height
        ViewGroup.LayoutParams lp = lv.getLayoutParams();
        lp.height = h + (lv.getDividerHeight() * (cnt - 1));
        lp.width=android.view.ViewGroup.LayoutParams.FILL_PARENT;
        lv.setLayoutParams(lp);
    }

//    public void SendMessage(SelectedMenuObj menu)
//    {
//    	Intent in = new Intent();
//        in.setAction("menuedit");
//        in.putExtra("menu", menu);
//        in.addCategory(Intent.CATEGORY_DEFAULT);
//        MyTable.this.sendBroadcast(in);
//    }
    //点击提交按钮
    public class ButtonOnclick implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
//			selectedProduct.setbState(true);
//			commitButton.setEnabled(false);
			String httpUrlString="http://"+getResources().getString(R.string.url_service);
			Declare declare=(Declare)MyTable.this.getApplicationContext();
			DeskObj deskObj=declare.curDeskObj;
			String returnString="";
			try {
				HttpDownloader.OrderForm(httpUrlString, deskObj);
//				returnString=HttpDownloader.OrderForm(httpUrlString, deskObj);
//				System.out.println(returnString);
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			
//			if(returnString!="")
//			{
//				String jsonString;
//				try {
//					jsonString = HttpDownloader.GetOrderForm(returnString);
//					System.out.println(jsonString);
//				} catch (ClientProtocolException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (JSONException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				
//			}
			HttpDownloader.ChangeOrderFormState(deskObj);
			v.setVisibility(View.GONE);
			UpdateElement();
		}
    	
    }
    public class OverBtnOnclick implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Declare declare=(Declare)MyTable.this.getApplicationContext();
			List<SelectedProduct> hiSelectedProducts=declare.getHistory().getHisSelectedProducts();
			if(hiSelectedProducts==null)
			{
				hiSelectedProducts=new ArrayList<SelectedProduct>();
			}
			final Calendar c = Calendar.getInstance();
			int mYear = c.get(Calendar.YEAR); //获取当前年份
			int mMonth = c.get(Calendar.MONTH);//获取当前月份
			int mDay = c.get(Calendar.DAY_OF_MONTH);//获取当前月份的日期号码
			int mHour = c.get(Calendar.HOUR_OF_DAY);//获取当前的小时数
			selectedProduct.setStrdate(mYear+"年"+mMonth+"月"+mDay+"日"+mHour+"时");
			hiSelectedProducts.add(selectedProduct);
			
			v.setVisibility(View.GONE);			
			declare.curDeskObj=null;
//			declare.getMenuListDataObj().RestoreHashMenuList();
			declare.getMenuListDataObj().RestoreCategoryMenuList();
			UpdateElement();
		}
    	
    }
    public class TableListAdapter extends SimpleAdapter {

		public ArrayList<HashMap<String, Object>> getmItemList() {
			return mItemList;
		}
		public void setmItemList(ArrayList<HashMap<String, Object>> hashList) {
			this.mItemList = hashList;
		}
		int count = 0;
		int[] idArray;
		List<SelectedMenuObj> menuList;
		Context thisContext;
	    private ArrayList<HashMap<String, Object>> mItemList;
	    public TableListAdapter(Context context, List<? extends Map<String, Object>> data,
	            int resource, String[] from, int[] to) {
	        super(context, data, resource, from, to);
	        thisContext=context;
	        mItemList = (ArrayList<HashMap<String, Object>>) data;
	        if(data == null){
	            count = 0;
	        }else{
	            count = data.size();
	        }
	        idArray=to;
	    }
	    public int getCount() {
	        return mItemList.size();
	    }

	    public Object getItem(int pos) {
	        return pos;
	    }

	    public long getItemId(int pos) {
	        return pos;
	    }
	    
	    public List<SelectedMenuObj> getMenuList() {
			return menuList;
		}
		public void setMenuList(List<SelectedMenuObj> menuList) {
			this.menuList = menuList;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			final View localView = super.getView(position, convertView, parent);
			final ListView listView=(ListView)parent;
			final int index=position;
	        
	        ImageView imgdelete=(ImageView)localView.findViewById(idArray[3]);
	        ImageView imgadd=(ImageView)localView.findViewById(idArray[4]);
	        
	        SelectedMenuObj menuObj=TableListAdapter.this.menuList.get(position);
	        Map<String, Object> mMap=TableListAdapter.this.mItemList.get(position);
            int mCount=Integer.parseInt(mMap.get("count").toString());
	        if(menuObj.isCommit)
	         {
	        	imgdelete.setVisibility(View.INVISIBLE);
	        	imgadd.setVisibility(View.INVISIBLE);
	         }
	        else {
	        	if(mCount<=menuObj.getLockCount())
	        	{
	        		imgdelete.setVisibility(View.INVISIBLE);
	        	}
	        	else {
	        		imgdelete.setVisibility(View.VISIBLE);
				}
	        	imgadd.setVisibility(View.VISIBLE);
			}
	        imgdelete.setTag(position);
	        imgadd.setTag(position);
	        
	        imgdelete.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
		             int position = Integer.parseInt(v.getTag().toString());
		             Map<String, Object> map=TableListAdapter.this.mItemList.get(position);
		             int count=Integer.parseInt(map.get("count").toString());
		             count--;
		             if(count<=0)
		             {
		            	 TableListAdapter.this.mItemList.remove(position); 
		             }
		             else {
						map.put("count", count+"");
					}
		             TableListAdapter.this.notifyDataSetChanged();
		             SelectedMenuObj menuObj=TableListAdapter.this.menuList.get(position);
		             menuObj.setCount(count);
		             Declare declare=(Declare)TableListAdapter.this.thisContext.getApplicationContext();
		             if(count<=0)
		             {
		            	 declare.curDeskObj.getSelectedProduct().DeleteMenu(menuObj);	
		             }
		             else {
		            	 menuObj.setTotalPrice(menuObj.getCount()*menuObj.getPrice());
		            	 declare.curDeskObj.getSelectedProduct().RemoveMenu(menuObj);
					}	
		            declare.getMenuListDataObj().ChangeMenuListByObj(menuObj);
//		            declare.getMenuListDataObj().ChangeHashList(menuObj);
		            MenuUtils.bUpdating=true;
		            sumTextView.setText(getResources().getString(R.string.infostr_sum)+selectedProduct.getSum());
		            if(TableListAdapter.this.mItemList.size()<=0)
		            {
		            	try {
							MyTable.this.UpdateElement();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
		            }
		            else {
		            	setListViewHeight(listView);
					}
				}
			});
	        
	        imgadd.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					int position = Integer.parseInt(v.getTag().toString());
		             Map<String, Object> map=TableListAdapter.this.mItemList.get(position);
		             int count=Integer.parseInt(map.get("count").toString());
		             count++;
		             map.put("count", count+"");
		             
		             TableListAdapter.this.notifyDataSetChanged();
		             SelectedMenuObj menuObj=TableListAdapter.this.menuList.get(position);
		             menuObj.setCount(count);
		             menuObj.setTotalPrice(menuObj.getCount()*menuObj.getPrice());
		             Declare declare=(Declare)TableListAdapter.this.thisContext.getApplicationContext();
		             declare.curDeskObj.getSelectedProduct().AddMenu(menuObj); 
		             declare.getMenuListDataObj().ChangeMenuListByObj(menuObj);
//			         declare.getMenuListDataObj().ChangeHashList(menuObj);
			         MenuUtils.bUpdating=true;
			         sumTextView.setText(getResources().getString(R.string.infostr_sum)+selectedProduct.getSum());	
			         setListViewHeight(listView);
//		             TableListAdapter.this.SendMessage(menuObj);
				}
			});
	        
	        localView.setOnTouchListener(new View.OnTouchListener() {
        		float sx,sy,ex,ey;
        		boolean action;
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					// TODO Auto-generated method stub
					if(event.getAction()==MotionEvent.ACTION_DOWN)
					{
						sx=event.getX();
						sy=event.getY();
						action=true;
					}
					else if(event.getAction()==MotionEvent.ACTION_MOVE)
					{
						ex=event.getX();
						ey=event.getY();
						if((ex-sx)>120&&action)
						{
							action=false;
							final SelectedMenuObj menuObj=TableListAdapter.this.menuList.get(index);
				            if(menuObj.isCommit())
				            {
				            	return true;
				            }
				            else {
								if(menuObj.getLockCount()>0)
								{
									return true;
								}
							}
							Animation animation=AnimationUtils.loadAnimation(MyTable.this, R.anim.delete_anim);
				            animation.setAnimationListener(new AnimationListener() {
								
								@Override
								public void onAnimationStart(Animation animation) {
									// TODO Auto-generated method stub
									
								}
								
								@Override
								public void onAnimationRepeat(Animation animation) {
									// TODO Auto-generated method stub
									
								}
								
								@Override
								public void onAnimationEnd(Animation animation) {
									// TODO Auto-generated method stub
									delete(menuObj);
								}
							});
				            localView.startAnimation(animation);
						}
						
					}
					return true;
				}
				public void delete(SelectedMenuObj menuObj)
				{
		            
		            TableListAdapter.this.mItemList.remove(index);
		            TableListAdapter.this.notifyDataSetChanged();
		             
		            Declare declare=(Declare)TableListAdapter.this.thisContext.getApplicationContext();
		            declare.curDeskObj.getSelectedProduct().DeleteMenu(menuObj);
		            
		            menuObj.setCount(0);
		            menuObj.setTotalPrice(0);
		            declare.getMenuListDataObj().ChangeMenuListByObj(menuObj);
		            MenuUtils.bUpdating=true;
		            sumTextView.setText(getResources().getString(R.string.infostr_sum)+selectedProduct.getSum());
		            if(TableListAdapter.this.mItemList.size()<=0)
		            {
		            	try {
							MyTable.this.UpdateElement();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
		            }
		            else {
		            	setListViewHeight(listView);
					} 
				}
			});
	        
			return localView;
		}
//		public void SendMessage(SelectedMenuObj menu)
//	    {
//	    	Intent in = new Intent();
//	        in.setAction("menuedit");
//	        in.putExtra("menu", menu);
//	        in.addCategory(Intent.CATEGORY_DEFAULT);
//	        TableListAdapter.this.thisContext.sendBroadcast(in);
//	    }
	}

}
